import UIKit
import  GrowingTextView
import SwiftEventBus
import Alamofire
import GradientLoadingBar

class SuggestViewController: UIViewController{
    
    var pageTitle = "问题反馈"
    var containerView: UIView!
    let scrollView = UIScrollView()
    let titleTextField = UITextField()
    let descriptionTextField = UITextField()
    let notchGradientLoadingBar = NotchGradientLoadingBar(
        height: 3.0
    )
    override func viewDidLoad() {
        navigationItem.title = pageTitle
        view.backgroundColor = primaryDarkBackgroundColor
        
        view.addSubview(scrollView)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-56)
        }
        containerView = UIView()
        scrollView.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(UIScreen.main.bounds.width)
        }
        
        
        let titleLabel = UILabel()
        titleLabel.font = UIFont.boldSystemFont(ofSize: 15)
        titleLabel.textColor = .white
        
        titleLabel.text = "联系方式"
        containerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints({ make in
            make.top.equalTo(containerView.safeAreaLayoutGuide.snp.top).offset(12)
            make.left.equalTo(containerView).offset(16)
        })
        
        
        containerView.addSubview(titleTextField)
        titleTextField.textColor = .white
        titleTextField.borderStyle = .roundedRect
        titleTextField.attributedPlaceholder  = NSAttributedString(
            string: "请输入联系方式，方便我们联系您",
            attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)]
        )
        titleTextField.backgroundColor = primaryBackgroundColor
        titleTextField.clearButtonMode = .whileEditing
        
        titleTextField.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.height.equalTo(42)
            make.left.equalTo(containerView).offset(16)
            make.width.equalTo(UIScreen.main.bounds.width-32)
            
        }
        
        
        let descriptionLabel = UILabel()
        descriptionLabel.font = UIFont.boldSystemFont(ofSize: 15)
        descriptionLabel.textColor = .white
        
        descriptionLabel.text = "意见建议"
        containerView.addSubview(descriptionLabel)
        descriptionLabel.snp.makeConstraints({ make in
            make.top.equalTo(titleTextField.snp.bottom).offset(12)
            make.left.equalTo(containerView).offset(16)
        })
        
        
        containerView.addSubview(descriptionTextField)
        descriptionTextField.textColor = .white
        descriptionTextField.attributedPlaceholder  = NSAttributedString(
            string: "请输入您的意见以及建议",
            attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)]
        )
        descriptionTextField.backgroundColor = primaryBackgroundColor
        descriptionTextField.borderStyle = .roundedRect
        descriptionTextField.textAlignment = .left
        descriptionTextField.clearButtonMode = .whileEditing
        
        descriptionTextField.snp.makeConstraints { make in
            make.top.equalTo(descriptionLabel.snp.bottom).offset(12)
            make.height.equalTo(72)
            make.left.equalTo(containerView).offset(16)
            make.width.equalTo(UIScreen.main.bounds.width-32)
        }
        
        
        
        
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.6){
            self.titleTextField.becomeFirstResponder()
        }
        
        containerView.snp.makeConstraints({ (make) in
            make.bottom.equalTo(descriptionTextField.snp.bottom)
            
        })
        
        self.view.isUserInteractionEnabled = true
        self.view.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(self.handleTap(_:))))
        
        
        let completionButton = UIButton(type: .custom)
        completionButton.setTitle("提交", for: .normal)
        completionButton.setTitleColor(.white, for: .normal)
        completionButton.layer.cornerRadius = 21
        completionButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
        completionButton.frame.size = CGSize(width: 68, height: 20)
        completionButton.backgroundColor = .primaryColor
        completionButton.addTarget(self, action: #selector(handleCompletionButtonTapped(_:)), for: .touchUpInside)
        self.view.addSubview(completionButton)
        completionButton.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-42)
            make.height.equalTo(42)
            make.width.equalTo(UIScreen.main.bounds.width*2/3)
            
        }
        
        
    }
    
    
    
    @objc private func handleCompletionButtonTapped(_ sender: Any) {
        if  var description = descriptionTextField.text{
            if description != "" {
                
                
                notchGradientLoadingBar.fadeIn()
                let url = "suggests/add"
                if let text = titleTextField.text{
                    description = "\(description)，联系方式：\(text)"
                }
                
                let parameters = SuggestParameters( content: description)
                AFManager.sharedInstance.manager.request("\(baseUrl)\(url)",method: .post,parameters: parameters,encoder: URLEncodedFormParameterEncoder.default).responseString { response in
                    self.notchGradientLoadingBar.fadeOut()
                    if response.value != nil{
                        self.view.makeToast("反馈成功",position: .center)
                        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.6){
                            self.navigationController?.popViewController(animated: true)
                        }
                        
                    }else{
                        
                    }
                }
                
            }else{
                self.view.makeToast("意见反馈不能为空",position: .center)
            }
        }
        
    }
    @objc private func handleTap(_ sender: Any) {
        self.view.endEditing(true)
        print("handleTap")
        
    }
    override var wx_titleTextAttributes: [NSAttributedString.Key : Any]? {
        return [NSAttributedString.Key.foregroundColor: UIColor.white]
    }
    
    
}
